package com.xpffly.design.mediator.service.impl;

import com.xpffly.design.mediator.service.AbstractMediator;

/**
 * @Author: xpengfei
 * @Date: 2021/4/5 20:14
 * @Description：具体中介者
 */
public class ConcertMediator extends AbstractMediator {

    @Override
    public void execute(String type, Object... objects) {
        // 采购电脑
        if ("purchase.buy".equalsIgnoreCase(type)) {
            this.byComputer((Integer) objects[0]);
        }
        // 销售电脑
        if ("sale.sell".equalsIgnoreCase(type)) {
            this.sellComputer((Integer) objects[0]);
        }
        // 折价销售
        if ("sale.offSell".equalsIgnoreCase(type)) {
            this.offSell();
        }
        // 清仓处理
        if ("stock.clear".equalsIgnoreCase(type)) {
            this.clearStock();
        }
    }

    /**
     * 采购电脑
     *
     * @param number
     */
    private void byComputer(int number) {
        // 销售情况良好，满额采购
        if (super.sale.getSaleStatus() > 80) {
            System.out.println("采购IBM电脑" + number + "台");
            super.stock.increase(number);
        } else {
            // 销售情况不好，折价采购
            System.out.println("采购IBM电脑" + number / 2 + "台");
            super.stock.increase(number / 2);
        }
    }

    /**
     * 销售电脑
     *
     * @param number
     */
    private void sellComputer(int number) {
        // 库存数量不足
        if (super.stock.getStockNumber() < number) {
            super.purchase.buyIBMComputer(number);
        }
        super.stock.decrease(number);
    }

    /**
     * 折价销售电脑
     */
    private void offSell() {
        System.out.println("折价销售IBM电脑" + super.stock.getStockNumber() + "台");
    }

    /**
     * 清仓处理
     */
    private void clearStock() {
        // 采购人员不要采购
        super.purchase.refuseBuyIBM();
        // 要求清仓销售
        super.sale.offSale();
    }
}
